﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using diploma.core.data;
using diploma.core;
using diploma.core.GUI;
using diploma.test.GUI;
using diploma.core.data.objects;
using diploma.editor;

namespace diploma
{
    internal delegate void LoadingDelegate();
    /// <summary>
    /// Часть класса MainForm, которая отвечает за загрузку :
    /// 1. Подключение плагинов(-)
    /// 2. Загрузку данных из бд(+)
    /// 3. Заполнение главного меню(+)
    /// 4. Локализации(+)
    /// </summary>
    partial class MainForm
    {
        System.Threading.Thread loadingThread;
        /// <summary>
        /// 
        /// </summary>
        private void Loading()
        {
            tslProcess.Text = _appLocalizator.GetLocolizeWord("loading");
            tsSpinner.Image = MainBlock.Properties.Resources.spinner;
            loadingThread = new System.Threading.Thread(new System.Threading.ThreadStart(loading));
            loadingThread.Name = "loading thread";
            loadingThread.IsBackground = true;
            loadingThread.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        private void loading()
        {
            try
            {
                if (this.InvokeRequired)
                {
                    DataProvider.Instance.AttachDataSource(new string[] { "localhost", "SYSDBA", "masterkey", "diploma_db.fdb" }, DataSourceTypes.FireBird);
                    Delegate loadingDelegate = new LoadingDelegate(loading);
                    tsMain.BeginInvoke(loadingDelegate, new object[] { });
                    return;
                }

                _items = new List<DiplomaForm>();
                DataList<IDataObject> qs = new DataList<IDataObject>(DataObjectTypes.Question);
                DataList<IDataObject> tr = new DataList<IDataObject>(DataObjectTypes.Term);
                DataList<IDataObject> pr = new DataList<IDataObject>(DataObjectTypes.Problem);
                DataList<IDataObject> ts = new DataList<IDataObject>(DataObjectTypes.Theorem);
                DataList<IDataObject> tm = new DataList<IDataObject>(DataObjectTypes.Theme);
                DataList<IDataObject> st = new DataList<IDataObject>(DataObjectTypes.Statement);
                DataList<IDataObject> pp = new DataList<IDataObject>(DataObjectTypes.Property);

                _items.Add(new TestForm(this, _appLocalizator, qs, tr, st, ts, pp, tm));
                _items.Add(new EditorForm(this, _appLocalizator, pr, ts, tm, st ));
                msMain.Items.AddRange(_items[0].MainMenuElements);
                msMain.Items.AddRange(_items[1].MainMenuElements);
                _items[0].MdiParent = this;
                _items[0].WindowState = System.Windows.Forms.FormWindowState.Maximized;
                _items[1].MdiParent = this;
                _items[1].WindowState = System.Windows.Forms.FormWindowState.Maximized;
                
                
                Localize();
                tsSpinner.Image = MainBlock.Properties.Resources.Ok;
                tslProcess.Text = _appLocalizator.GetLocolizeWord("loaded");
                loadingThread.Interrupt();               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }



    }
}
